#!/bin/bash
function enable_local_yum(){
    echo "启用本地YUM源" >>$logs_file
    whiptail --backtitle "Author: Elvis" --title "提示" --yesno \
               "启用本地YUM源仓库，则会禁用其他的YUM仓库，是否继续？" 10 60
    if [ "$?" != "0" ];then
        sleep 0.15
        return 1
    fi
    
    rm -rf /etc/yum.repos.d/CentOS-Local.repo
    
    whiptail --backtitle "Author: Elvis" --title "注意" --msgbox "请在继续之前，将系统安装镜像，如CentOS-7-x86_64-DVD-1810.iso，拷贝到系统中或者直接挂载到光驱中\n\
    镜像版本需要和你的系统版本一致，否则YUM安装会存在一系列依赖问题！！！" 12 60
    isoLocation=
    isoLocation=$(whiptail --backtitle "Author: Elvis" --title "镜像位置" --inputbox "请输入镜像文件所在目录，如果直接挂载在光驱中，则留空" 10 60 3>&1 1>&2 2>&3)
    if [ "$?" != "0" ];then
        sleep 0.15
        return 1
    fi
    isoLocation=`echo $isoLocation`
    {
    echo 1
    sleep 1
    msg="挂载位置检查"
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n10\n$msg\nXXX"
    echo 10
    if [ -n "$isoLocation" ];then
        isoName=`ls $isoLocation 2>>$logs_file|grep -E ".*\.iso$"|head -n 1`
        if [ -z "$isoName" ];then
          msg="错误！！！给定的目录没有ISO镜像文件"
          echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
          echo "$msg" >/tmp/.localyum.err.info
          echo -e "XXX\n20\n$msg\nXXX"
          echo 20
          sleep 3
          return 1
        else
          source_point="$isoLocation/$isoName"
        fi
    else
        source_point="/dev/cdrom"
    fi
    
    msg="创建挂载点"
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n30\n$msg\nXXX"
    echo 30
    if [ -w /mnt/ ];then
        repoDir=/mnt/LocalYum
        if [ -d /mnt/LocalYum ];then
          umount -f /mnt/LocalYum >/dev/null 2>&1
        else
          mkdir -p /mnt/LocalYum >/dev/null 2>&1
        fi
    else
        repoDir=/tmp/LocalYum
        if [ -d /tmp/LocalYum ];then
          umount -f /tmp/LocalYum >/dev/null 2>&1
        else
          mkdir -p /tmp/LocalYum >/dev/null 2>&1
        fi
    fi
    
    msg="挂载镜像文件"
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n50\n$msg\nXXX"
    echo 50
    mount -o loop $source_point $repoDir >>$logs_file 2>&1
    
    Packages_dir=`find $repoDir -name "Packages" -type d|head -n 1`
    
    if [ -n "$Packages_dir" ];then
        msg="配置YUM源"
        echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
        echo -e "XXX\n80\n$msg\nXXX"
        echo 80
        msg="备份之前的仓库"
        echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
        for i in `ls /etc/yum.repos.d/*.repo 2>/dev/null`
        do
          /bin/mv -f $i $i.bak >>$logs_file 2>&1
        done
    
        cat > /etc/yum.repos.d/CentOS-Local.repo <<EOF
[CentOS-Local]
name=CentOS-$releasever - From Media
baseurl=file://$repoDir
enabled=1
gpgcheck=0
EOF
        yum clean all >/dev/null 2>&1
    else
        msg="挂载镜像到系统失败！！！"
        echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
        echo -e "XXX\n80\n$msg\nXXX"
        echo 80
        echo "$msg" >/tmp/.localyum.err.info
        sleep 3
        return 1
    fi
    echo 100
    sleep 1
    }| whiptail --backtitle "Author: Elvis" --title "配置本地YUM仓库" --gauge " " 6 60 0
    
        
    if [ ! -f /etc/yum.repos.d/CentOS-Local.repo ];then
        errInfo=`cat /tmp/.localyum.err.info 2>/dev/null`
        rm -rf /tmp/.localyum.err.info
        msg="配置本地YUM仓库失败，请检查日志文件！\n\n$errInfo"
        echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
        whiptail --backtitle "Author: Elvis" --title "错误" --msgbox "$msg" 10 60
        return 1
        sleep 0.15
    fi
    
    yum repolist |grep "CentOS-Local" >/dev/null 2>&1
    if [ "$?" == "0" ];then
        msg="配置本地YUM仓库配置成功，赶快用yum命令安装软件试试吧！"
        echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
        echo "">>$logs_file
        whiptail --backtitle "Author: Elvis" --title "信息" --msgbox "$msg" 10 60
        sleep 0.15
    fi      
}

function disable_local_yum() {
    echo "禁用本地YUM源" >>$logs_file
    whiptail --backtitle "Author: Elvis" --title "提示" --yesno \
               "此操作会将/etc/yum.repos.d中.repo.bak文件还原，并删除\nCentOS-Local.repo文件\n\n是否继续？" 12 60
    if [ "$?" != "0" ];then
        sleep 0.15
        return 1
    fi
    msg="还原repo.bak为repo"
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    for i in `ls /etc/yum.repos.d/*.repo.bak 2>/dev/null`
    do
        /bin/mv -f $i ${i//.bak/} >>$logs_file 2>&1
    done
    rm -rf /etc/yum.repos.d/CentOS-Local.repo
    msg="还原YUM源完成"
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo "">>$logs_file
    whiptail --backtitle "Author: Elvis" --title "信息" --msgbox "$msg" 10 60
}


function yum_options() {
    yumOptions=$(whiptail --backtitle "Author: Elvis" --title "选项" --radiolist \
    "请选择要执行的操作?" 12 60 2 \
    "1" "启用本地YUM源                        " ON \
    "2" "停用本地YUM源" OFF 3>&1 1>&2 2>&3)
    if [ "$?" != "0" ];then
        sleep 1
        return 1
    else
        if [ "$yumOptions" == "1" ];then
          enable_local_yum
        else
          disable_local_yum
        fi
    fi
}



